[impeller] use cullRect for drawAtlas coverage (flutter/engine#35133)

This commit is contained in:
Jonah Williams
2022-08-03 13:27:04 -07:00
committed by GitHub
parent 3e485ec15e
commit 2604d38c3d
4 changed files with 51 additions and 2 deletions

View File

@@ -346,8 +346,8 @@ void Canvas::DrawAtlas(std::shared_ptr<Image> atlas,
contents->SetTexture(atlas->GetTexture());
contents->SetSamplerDescriptor(std::move(sampler));
contents->SetBlendMode(blend_mode);
contents->SetCullRect(cull_rect);
contents->SetAlpha(paint.color.alpha);
// TODO(jonahwilliams): set cull rect.
Entity entity;
entity.SetTransformation(GetCurrentTransformation());

View File

@@ -50,7 +50,15 @@ void AtlasContents::SetBlendMode(Entity::BlendMode blend_mode) {
blend_mode_ = blend_mode;
}
void AtlasContents::SetCullRect(std::optional<Rect> cull_rect) {
cull_rect_ = cull_rect;
}
std::optional<Rect> AtlasContents::GetCoverage(const Entity& entity) const {
if (cull_rect_.has_value()) {
return cull_rect_.value().TransformBounds(entity.GetTransformation());
}
Rect bounding_box = {};
for (size_t i = 0; i < texture_coords_.size(); i++) {
auto matrix = transforms_[i];

View File

@@ -33,6 +33,8 @@ class AtlasContents final : public Contents {
void SetColors(std::vector<Color> colors);
void SetCullRect(std::optional<Rect> cull_rect);
void SetSamplerDescriptor(SamplerDescriptor desc);
void SetAlpha(Scalar alpha);
@@ -53,6 +55,7 @@ class AtlasContents final : public Contents {
std::vector<Color> colors_;
std::vector<Matrix> transforms_;
Entity::BlendMode blend_mode_;
std::optional<Rect> cull_rect_;
Scalar alpha_ = 1.0;
SamplerDescriptor sampler_descriptor_ = {};

View File

@@ -1159,6 +1159,44 @@ TEST_P(EntityTest, DrawAtlasWithColor) {
ASSERT_TRUE(OpenPlaygroundHere(e));
}
TEST_P(EntityTest, DrawAtlasUsesProvidedCullRectForCoverage) {
auto atlas = CreateTextureForFixture("bay_bridge.jpg");
auto size = atlas->GetSize();
Scalar half_width = size.width / 2;
Scalar half_height = size.height / 2;
std::vector<Rect> texture_coordinates = {
Rect::MakeLTRB(0, 0, half_width, half_height),
Rect::MakeLTRB(half_width, 0, size.width, half_height),
Rect::MakeLTRB(0, half_height, half_width, size.height),
Rect::MakeLTRB(half_width, half_height, size.width, size.height)};
std::vector<Matrix> transforms = {
Matrix::MakeTranslation({0, 0, 0}),
Matrix::MakeTranslation({half_width, 0, 0}),
Matrix::MakeTranslation({0, half_height, 0}),
Matrix::MakeTranslation({half_width, half_height, 0})};
std::shared_ptr<AtlasContents> contents = std::make_shared<AtlasContents>();
contents->SetTransforms(std::move(transforms));
contents->SetTextureCoordinates(std::move(texture_coordinates));
contents->SetTexture(atlas);
contents->SetBlendMode(Entity::BlendMode::kSource);
auto transform = Matrix::MakeScale(GetContentScale());
Entity e;
e.SetTransformation(transform);
e.SetContents(contents);
ASSERT_EQ(contents->GetCoverage(e).value(),
Rect::MakeSize(size).TransformBounds(transform));
contents->SetCullRect(Rect::MakeLTRB(0, 0, 10, 10));
ASSERT_EQ(contents->GetCoverage(e).value(),
Rect::MakeLTRB(0, 0, 10, 10).TransformBounds(transform));
}
TEST_P(EntityTest, DrawAtlasWithOpacity) {
// Draws the image as four squares stiched together slightly
// opaque
@@ -1184,8 +1222,8 @@ TEST_P(EntityTest, DrawAtlasWithOpacity) {
contents->SetTransforms(std::move(transforms));
contents->SetTextureCoordinates(std::move(texture_coordinates));
contents->SetTexture(atlas);
contents->SetAlpha(0.5);
contents->SetBlendMode(Entity::BlendMode::kSource);
contents->SetAlpha(0.5);
Entity e;
e.SetTransformation(Matrix::MakeScale(GetContentScale()));